		TITLE	INITMAP	- Copyright (c) SLR Systems 1994

		INCLUDE	MACROS
		INCLUDE	WIN32DEF

		PUBLIC	INITIALIZE_WRITEMAP


		.DATA

		EXTERNDEF	SYMBOL_TABLE_USE:BYTE,EXPORT_TABLE_USE:BYTE,IMPORT_TABLE_USE:BYTE,MDB_TABLE_USE:BYTE

		EXTERNDEF	MAPFILE_GINDEX:DWORD,MAP_THREAD_ID:DWORD,MAP_THREAD_HANDLE:DWORD

		EXTERNDEF	SEGS_ORDERED_SEM:QWORD,PUBS_DEFINED_SEM:QWORD,START_DEFINED_SEM:QWORD,XREF_OK_SEM:QWORD
		EXTERNDEF	MAPLINES_OK_SEM:QWORD,EXPS_DEFINED_SEM:QWORD


		.CODE	MIDDLE_TEXT

		EXTERNDEF	_capture_eax:proc
		EXTERNDEF	_err_abort:proc,DO_SEGMENTS_GROUPS_MAP:PROC,SAY_VERBOSE:PROC,CAPTURE_EAX:PROC,DO_EXPORTS_MAP:PROC
		EXTERNDEF	DO_SYMBOLS_MAP:PROC,DO_XREF_MAP:PROC,DO_LINENUMBERS_MAP:PROC,DO_START_ADDRESS:PROC
		EXTERNDEF	RELEASE_DSBX:PROC,XCNOTIFY:PROC

		EXTERNDEF	THREAD_FAIL_ERR:ABS


INITIALIZE_WRITEMAP	PROC
		;
		;SET UP FOR MAP OUTPUT IN PIECES
		;
		MOV	AL,1
		MOV	SYMBOL_TABLE_USE,AL	;.EXE OUTPUT IS MAIN USER
if	fg_segm
		MOV	EXPORT_TABLE_USE,AL	;.EXE OUTPUT IS MAIN USER
		MOV	IMPORT_TABLE_USE,AL	;.EXE OUTPUT IS MAIN USER
endif

if	fg_td

		BITT	TD_FLAG
		JZ	L1$
		INC	IMPORT_TABLE_USE
		INC	MDB_TABLE_USE
L1$:

endif

if	fg_cv
		BITT	CODEVIEW_FLAG
		JZ	L2$
		INC	MDB_TABLE_USE
L2$:

endif

if	fgh_mapthread
		BITT	_HOST_THREADED
		JZ	L3$

		MOV	EAX,OFF SEGS_ORDERED_SEM	;MAKE SURE THREAD WAITS
		CALL	CAPTURE_EAX

		MOV	EAX,OFF EXPS_DEFINED_SEM
		CALL	CAPTURE_EAX

		MOV	EAX,OFF PUBS_DEFINED_SEM	;PUBLICS DEFINED
		CALL	CAPTURE_EAX

		MOV	EAX,OFF MAPLINES_OK_SEM
		CALL	CAPTURE_EAX

		MOV	EAX,OFF START_DEFINED_SEM	;START ADDRESS DEFINED
		CALL	CAPTURE_EAX

		MOV	EAX,OFF XREF_OK_SEM		;DONE WITH SYMBOL ADDRESSES...
		CALL	CAPTURE_EAX
if	any_overlays
		MOV	EAX,OFF SECTIONMAP_OK_SEM
		CALL	CAPTURE_EAX
endif
endif
L3$:
		MOV	EAX,MAPFILE_GINDEX

		OR	EAX,EAX
		JNZ	DO_MAPFILE

		RET

DO_MAPFILE:

		MOV	EAX,OFF WRITING_MAP_MSG
		CALL	SAY_VERBOSE
if	fgh_mapthread
		;
		;RUN MAP OUTPUT AS SEPARATE THREAD
		;
		BITT	_HOST_THREADED
		JZ	L9$

		PUSH	OFF MAP_THREAD_ID
		PUSH	0

		PUSH	0		;PARAMETER...
		PUSH	OFF DO_MAP_OS2

		PUSH	4K		;STACK COMMIT SIZE
		PUSH	0		;SECURITY

		CALL	CreateThread

		TEST	EAX,EAX
		JNZ	OS2_OK
DO_MAP_FAIL:
		MOV	AL,THREAD_FAIL_ERR
		push	EAX
		call	_err_abort

OS2_OK:
		MOV	MAP_THREAD_HANDLE,EAX

endif

L9$:
		CALL	ADJUST_FLAGS

		RET

INITIALIZE_WRITEMAP	ENDP


ADJUST_FLAGS	PROC	NEAR
		;
		;OK, COUNT MY NEEDS FOR TABLES
		;
		BITT	SEGMENTS_MAP
		JZ	L1$
		BITT	DETAILEDMAP_FLAG
		JZ	L1$
		INC	SYMBOL_TABLE_USE	;I NEED SYMBOL TABLE FOR DETAILED MAP OUTPUT
L1$:
		BITT	SYMBOLS_OUT
		JZ	L2$
		INC	SYMBOL_TABLE_USE	;I NEED SYMBOL TABLE FOR SYMBOL MAP
if	fg_segm OR fg_pe
		INC	EXPORT_TABLE_USE	;I NEED EXPORTS FOR SYMBOL MAP
		INC	IMPORT_TABLE_USE	;I NEED IMPORTS FOR SYMBOL MAP
endif
L2$:
		BITT	XREF_OUT
		JZ	L3$
		INC	SYMBOL_TABLE_USE	;I NEED SYMBOL TABLE FOR XREF USE
L3$:

		BITT	LINENUMBERS_FLAG
		JZ	L4$
		INC	MDB_TABLE_USE
L4$:
		RET

ADJUST_FLAGS	ENDP


if	fgh_mapthread

DO_MAP_OS2	PROC
		;
		;OUTPUT PARTS OF MAP FILE...
		;
		PUSHM	-1,0
		PUSH	OFF XCNOTIFY
		ASSUME	FS:NOTHING

		PUSH	DWORD PTR FS:[0]
		MOV	FS:[0],ESP

		CALL	DO_SEGMENTS_GROUPS_MAP	;IF REQUESTED AND READY
if	fg_segm OR fg_pe
		CALL	DO_EXPORTS_MAP		;IF REQUESTED
endif
		CALL	DO_SYMBOLS_MAP		;IF REQUESTED AND READY
		CALL	DO_LINENUMBERS_MAP	;IF REQUESTED AND READY
		CALL	DO_XREF_MAP		;IF REQUESTED AND READY
if	any_overlays
		CALL	DO_PRINT_SECTIONS
endif
		CALL	DO_START_ADDRESS	;IF READY

		;
		;NOW STOP...
		;
		PUSH	0
		CALL	ExitThread

DO_MAP_OS2	ENDP

endif


		.CONST

WRITING_MAP_MSG DB	SIZEOF WRITING_MAP_MSG-1,'Writing .MAP file',0DH,0AH


		END

